package com.ninong.ker.admin.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ninong.ker.admin.domain.Role;
import com.ninong.ker.admin.domain.RoleMenu;
import com.ninong.ker.admin.domain.UserRole;
import com.ninong.ker.admin.mapper.RoleMapper;
import com.ninong.ker.admin.mapper.RoleMenuMapper;
import com.ninong.ker.admin.mapper.UserRoleMapper;
import com.ninong.ker.admin.service.RoleService;
import com.ninong.ker.admin.service.dto.RoleDTO;
import com.ninong.ker.common.exception.CmdException;
import com.ninong.ker.common.tools.Constants;
import com.ninong.ker.common.tools.enums.BusinessEnum;

/**
 * 
 * @author cmd
 * @date 2020年11月29日 上午11:12:45
 */
@Service
@org.apache.dubbo.config.annotation.Service(version = "1.0.0")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;

	@Override
	public PageInfo<Role> listPage(RoleDTO dto) {
		PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
		QueryWrapper<Role> query = new QueryWrapper<>();
		if (!StringUtils.isEmpty(dto.getName())) {
			query.like("name", dto.getName());
		}
		List<Role> roles = baseMapper.selectList(query);
		return new PageInfo<Role>(roles);
	}

	@Override
	public void delete(String[] ids) {
		List<String> idList = Arrays.asList(ids);
		idList.forEach(id -> {
			Integer count = userRoleMapper.selectCount(new QueryWrapper<UserRole>().eq("role_id", id));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.ROLE_USER_EXIST);
			}
			count = roleMenuMapper.selectCount(new QueryWrapper<RoleMenu>().eq("role_id", id));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.ROLE_MENU_EXIST);
			}
		});
		baseMapper.deleteBatchIds(idList);
	}

	@Override
	public void create(Role entity) {
		Integer count = null;
		if (StringUtils.isEmpty(entity.getId())) {
			count = baseMapper.selectCount(new QueryWrapper<Role>().eq("name", entity.getName()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.ROLE_NAME_REPEAT);
			}
		} else {
			count = baseMapper
					.selectCount(new QueryWrapper<Role>().ne("id", entity.getId()).eq("name", entity.getName()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.ROLE_NAME_REPEAT);
			}

		}
		saveOrUpdate(entity);
	}

	@Override
	public Role get(String id) {
		Role entity = baseMapper.selectById(id);
		return entity;
	}

	@Override
	public List<Role> listRoleTree() {
		return baseMapper.selectList(new QueryWrapper<>());
	}

	@Override
	public List<String> listTreeByUserId(String userId) {
		QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", userId);
		List<UserRole> list = userRoleMapper.selectList(queryWrapper);
		List<String> checkedIds = list.stream().map(UserRole::getRoleId).collect(Collectors.toList());
		return checkedIds;
	}

	@Override
	public void merge(String userId, List<String> roleIds) {
		QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", userId);
		userRoleMapper.delete(queryWrapper);
		roleIds.forEach(id -> {
			UserRole entity = new UserRole(userId, id);
			userRoleMapper.insert(entity);
		});
	}

}
